package com.ggj2013.hbh.utils;

import com.eclecticdesignstudio.motion.Actuate;
import com.ggj2013.hbh.LevelScreen;
import com.ggj2013.hbh.Player;
import nme.events.TimerEvent;
import nme.media.Sound;
import nme.media.SoundChannel;
import nme.media.SoundTransform;
import nme.events.Event;
import nme.Assets;
import nme.Lib;
import nme.utils.Timer;

/**
 * ...
 * @author Drew Nicolo
 */

class SoundPlayer 
{
	// Channels
	public static var bgmChannel:SoundChannel;
	public static var subSoundChannel:SoundChannel;
	public static var sfxChannel1:SoundChannel;
	public static var sfxChannel2:SoundChannel;
	public static var sfxChannel3:SoundChannel;
	public static var footstepChannel1:SoundChannel;
	public static var footstepChannel2:SoundChannel;
	public static var buttonChannel:SoundChannel;
	public static var heartbeatChannel:SoundChannel;
	
	//Music
	public static var subSounds:Sound;
	public static var ambience1:Sound;
	public static var ambience2:Sound;
	public static var ambience3:Sound;
	public static var ambience4:Sound;
	
	//SFX
	public static var heartbeat1:Sound;
	public static var heartbeat2:Sound;
	public static var heartbeat3:Sound;
	public static var heartbeat4:Sound;
	public static var heartbeat5:Sound;
	
	public static var footstep1:Sound;
	public static var footstep2:Sound;
	public static var footstep3:Sound;
	public static var footstep4:Sound;
	public static var footstep5:Sound;
	public static var footstep6:Sound;
	public static var footstep7:Sound;
	public static var footstep8:Sound;
	public static var footstep9:Sound;
	public static var footstep10:Sound;
	public static var footstep11:Sound;
	public static var footstep12:Sound;
	public static var footstep13:Sound;
	public static var footstep14:Sound;
	public static var footstep15:Sound;
	public static var footstep16:Sound;
	
	//Variables
	public static var distance:Int;
	public static var currentTrack:Int;
	public static var isFootstep1playing:Bool;
	public static var isFootstep2playing:Bool;
	public static var initialized:Bool;
	public static var activeFootstepTrack:Int;
	
	//Constants
	public static var DISTANCE_LEVEL1:Int = 100;
	public static var DISTANCE_LEVEL2:Int = 200;
	public static var DISTANCE_LEVEL3:Int = 300;
	public static var DISTANCE_LEVEL4:Int = 400;
	public static var DISTANCE_LEVEL5:Int = 500;
	public static var SUB_SFX_VOLUME:Float = 0.4;
	public static var AMBIENCE_VOLUME:Float = 0.3;
	public static var FOOTSTEP_VOLUME:Float = 0.3;
	
	//Sound Transform
	public static var transform:SoundTransform;

	public function new() 
	{
		//Never called
	}
	
	public static function initialize():Void
	{
		if (!initialized)
		{
			//Setup sound channels
			bgmChannel = new SoundChannel();
			subSoundChannel = new SoundChannel();
			sfxChannel1 = new SoundChannel();
			sfxChannel2 = new SoundChannel();
			sfxChannel3 = new SoundChannel();
			footstepChannel1 = new SoundChannel();
			footstepChannel2 = new SoundChannel();
			buttonChannel = new SoundChannel();
			heartbeatChannel = new SoundChannel();
			
			//Load music and SFX
			loadAllSounds();
			
			isFootstep1playing = false;
			isFootstep2playing = false;
			activeFootstepTrack = 1;
			setCurrentTrack(1);
			playCurrentTrack();
			playSubSFX();
			//setVolumeLevels();
			
			initialized = true;
		}
	}
	
	public static function loadAllSounds():Void
	{
		//Music
		subSounds = Assets.getSound ("assets/sound/ambience_engineWhirr.mp3");
		ambience1 = Assets.getSound ("assets/sound/ambience_1.mp3");
		ambience2 = Assets.getSound ("assets/sound/ambience_2.mp3");
		ambience3 = Assets.getSound ("assets/sound/ambience_3.mp3");
		ambience4 = Assets.getSound ("assets/sound/ambience_4.mp3");
		
		//Sounds
		heartbeat1 = Assets.getSound ("assets/sound/heartbeat1.mp3");
		heartbeat2 = Assets.getSound ("assets/sound/heartbeat2.mp3");
		heartbeat3 = Assets.getSound ("assets/sound/heartbeat3.mp3");
		heartbeat4 = Assets.getSound ("assets/sound/heartbeat4.mp3");
		heartbeat5 = Assets.getSound ("assets/sound/heartbeat5.mp3");
		footstep1 = Assets.getSound ("assets/sound/step1.mp3");
		footstep2 = Assets.getSound ("assets/sound/step2.mp3");
		footstep3 = Assets.getSound ("assets/sound/step3.mp3");
		footstep4 = Assets.getSound ("assets/sound/step4.mp3");
		footstep5 = Assets.getSound ("assets/sound/step5.mp3");
		footstep6 = Assets.getSound ("assets/sound/step6.mp3");
		footstep7 = Assets.getSound ("assets/sound/step7.mp3");
		footstep8 = Assets.getSound ("assets/sound/step8.mp3");
		footstep9 = Assets.getSound ("assets/sound/step9.mp3");
		footstep10 = Assets.getSound ("assets/sound/step10.mp3");
		footstep11 = Assets.getSound ("assets/sound/step11.mp3");
		footstep12 = Assets.getSound ("assets/sound/step12.mp3");
		footstep13 = Assets.getSound ("assets/sound/step13.mp3");
		footstep14 = Assets.getSound ("assets/sound/step14.mp3");
		footstep15 = Assets.getSound ("assets/sound/step15.mp3");
		footstep16 = Assets.getSound ("assets/sound/step16.mp3");
	}
	
	public static function playSubSFX(evt:Event = null):Void
	{
		subSoundChannel = subSounds.play();
		transform = new SoundTransform();
		transform.volume = SUB_SFX_VOLUME;
		subSoundChannel.soundTransform = transform;
		
		subSoundChannel.addEventListener(Event.SOUND_COMPLETE, playSubSFX);
	}
	
	public static function playFootsteps1():Void
	{
		if (!isFootstep1playing)
		{
			var stepType = Tools.randomInt(17, 1);
			
			if (stepType == 1)
			{
				footstepChannel1 = footstep1.play();
			}
			else if (stepType == 2)
			{
				footstepChannel1 = footstep2.play();
			}
			else if (stepType == 3)
			{
				footstepChannel1 = footstep3.play();
			}
			else if (stepType == 4)
			{
				footstepChannel1 = footstep4.play();
			}
			else if (stepType == 5)
			{
				footstepChannel1 = footstep5.play();
			}
			else if (stepType == 6)
			{
				footstepChannel1 = footstep6.play();
			}
			else if (stepType == 7)
			{
				footstepChannel1 = footstep7.play();
			}
			else if (stepType == 8)
			{
				footstepChannel1 = footstep8.play();
			}
			else if (stepType == 9)
			{
				footstepChannel1 = footstep9.play();
			}
			else if (stepType == 10)
			{
				footstepChannel1 = footstep10.play();
			}
			else if (stepType == 11)
			{
				footstepChannel1 = footstep11.play();
			}
			else if (stepType == 12)
			{
				footstepChannel1 = footstep12.play();
			}
			else if (stepType == 13)
			{
				footstepChannel1 = footstep13.play();
			}
			else if (stepType == 14)
			{
				footstepChannel1 = footstep14.play();
			}
			else if (stepType == 15)
			{
				footstepChannel1 = footstep15.play();
			}
			else if (stepType == 16)
			{
				footstepChannel1 = footstep16.play();
			}
			else
			{
				footstepChannel1 = footstep1.play();
			}
			
			isFootstep1playing = true;
			activeFootstepTrack = 2;
			transform.volume = FOOTSTEP_VOLUME;
			footstepChannel1.soundTransform = transform;
		}
		
		footstepChannel1.addEventListener(Event.SOUND_COMPLETE, footstep1Complete);
	}
	
	public static function playFootsteps2():Void
	{
		if (!isFootstep2playing)
		{
			var stepType = Tools.randomInt(6, 1);
			
			if (stepType == 1)
			{
				footstepChannel2 = footstep1.play();
			}
			else if (stepType == 2)
			{
				footstepChannel2 = footstep2.play();
			}
			else if (stepType == 3)
			{
				footstepChannel2 = footstep3.play();
			}
			else if (stepType == 4)
			{
				footstepChannel2 = footstep4.play();
			}
			else if (stepType == 5)
			{
				footstepChannel2 = footstep5.play();
			}
			else if (stepType == 6)
			{
				footstepChannel2 = footstep6.play();
			}
			else if (stepType == 7)
			{
				footstepChannel2 = footstep7.play();
			}
			else if (stepType == 8)
			{
				footstepChannel2 = footstep8.play();
			}
			else if (stepType == 9)
			{
				footstepChannel2 = footstep9.play();
			}
			else if (stepType == 10)
			{
				footstepChannel2 = footstep10.play();
			}
			else if (stepType == 11)
			{
				footstepChannel2 = footstep11.play();
			}
			else if (stepType == 12)
			{
				footstepChannel2 = footstep12.play();
			}
			else if (stepType == 13)
			{
				footstepChannel2 = footstep13.play();
			}
			else if (stepType == 14)
			{
				footstepChannel2 = footstep14.play();
			}
			else if (stepType == 15)
			{
				footstepChannel2 = footstep15.play();
			}
			else if (stepType == 16)
			{
				footstepChannel2 = footstep16.play();
			}
			else
			{
				footstepChannel2 = footstep1.play();
			}
			
			isFootstep2playing = true;
			activeFootstepTrack = 1;
			transform.volume = FOOTSTEP_VOLUME;
			footstepChannel2.soundTransform = transform;
		}
		
		footstepChannel2.addEventListener(Event.SOUND_COMPLETE, footstep2Complete);
	}
	
	public static function footstep1Complete(evt:Event = null)
	{
		isFootstep1playing = false;
	}
	
	public static function footstep2Complete(evt:Event = null)
	{
		isFootstep2playing = false;
	}
	
	public static function stopFootsteps():Void
	{
		footstepChannel1.stop();
		footstepChannel2.stop();
		isFootstep1playing = false;
		isFootstep2playing = false;
	}
	
	public static function playCurrentTrack(evt:Event = null):Void
	{
		bgmChannel.stop();
		switch (currentTrack)
		{
			case 1:
				bgmChannel = ambience1.play();
			case 2:
				bgmChannel = ambience2.play();
			case 3:
				bgmChannel = ambience3.play();
			case 4:
				bgmChannel = ambience4.play();
			default:
				bgmChannel = ambience1.play();
		}
		transform = new SoundTransform();
		transform.volume = AMBIENCE_VOLUME;
		bgmChannel.soundTransform = transform;
		
		bgmChannel.addEventListener(Event.SOUND_COMPLETE, playCurrentTrack);
	}
	
	public static function setCurrentTrack(_newTrack:Int):Void
	{
		currentTrack = _newTrack;
		
		playCurrentTrack();
	}
	
	public static function setVolumeLevels():Void
	{
		transform = new SoundTransform();
		transform.volume = SUB_SFX_VOLUME;
		subSoundChannel.soundTransform = transform;
		
		transform.volume = AMBIENCE_VOLUME;
		bgmChannel.soundTransform = transform;
		
		//Actuate.tween(transform, 1, { volume: 0}).onUpdate (applySoundTransform).onComplete(soundTweenComplete);
	}
	
	public static function checkSanity():Void
	{
		//Sanity level1
		if ((LevelScreen.player.currentSanity > Player.MAX_SANITY * 0.8) && currentTrack != 1)
		{
			setCurrentTrack(1);
		}
		else if ((LevelScreen.player.currentSanity > Player.MAX_SANITY * 0.6) && (LevelScreen.player.currentSanity < Player.MAX_SANITY * 0.8) && currentTrack != 2)
		{
			setCurrentTrack(2);
		}
		else if ((LevelScreen.player.currentSanity > Player.MAX_SANITY * 0.4) && (LevelScreen.player.currentSanity < Player.MAX_SANITY * 0.6) && currentTrack != 3)
		{
			setCurrentTrack(3);
		}
		else if ((LevelScreen.player.currentSanity > Player.MAX_SANITY * 0.2) && (LevelScreen.player.currentSanity < Player.MAX_SANITY * 0.4) && currentTrack != 4)
		{
			setCurrentTrack(4);
		}
		else if ((LevelScreen.player.currentSanity < Player.MAX_SANITY * 0.2) && currentTrack != 4)
		{
			setCurrentTrack(4);
		}
	}
	
	public static function setHeartbeatDistance(_distance:Float):Void
	{
		if (_distance > DISTANCE_LEVEL5)
		{
			distance = 1;
		}
		else if (_distance > DISTANCE_LEVEL4 && _distance < DISTANCE_LEVEL5)
		{
			distance = 2;
		}
		else if (_distance > DISTANCE_LEVEL3 && _distance < DISTANCE_LEVEL4)
		{
			distance = 3;
		}
		else if (_distance > DISTANCE_LEVEL2 && _distance < DISTANCE_LEVEL3)
		{
			distance = 4;
		}
		else if (_distance > DISTANCE_LEVEL1 && _distance < DISTANCE_LEVEL2)
		{
			distance = 5;
		}
		else if (_distance < DISTANCE_LEVEL1)
		{
			distance = 5;
		}
		else
		{
			distance = 3;
		}
	}
	
	public static function stopEndingSFX():Void
	{
		subSoundChannel.stop();
		sfxChannel1.stop();
		sfxChannel2.stop();
		sfxChannel3.stop();
		footstepChannel1.stop();
		footstepChannel2.stop();
		buttonChannel.stop();
		initialized = false;
	}
	
	public static function stopMusic():Void
	{
		bgmChannel.stop();
	}
	
	public static function stopHeartbeatSFX():Void
	{
		heartbeatChannel.stop();
	}
	
	public static function updateHeartbeatVolume(_distance:Float):Void
	{
		//tackle this once we get rooms more figured out
	}
	
	public static function playHeartbeatSFX(evt:Event = null):Void
	{
		switch (distance)
		{
			case 1:
				heartbeatChannel = heartbeat1.play();
			case 2:
				heartbeatChannel = heartbeat2.play();
			case 3:
				heartbeatChannel = heartbeat3.play();
			case 4:
				heartbeatChannel = heartbeat4.play();
			case 5:
				heartbeatChannel = heartbeat5.play();
			default:
				heartbeatChannel = heartbeat3.play();
		}
		
		heartbeatChannel.addEventListener(Event.SOUND_COMPLETE, playHeartbeatSFX);
		
	}
	
}